//
//  MineViewModel.swift
//  LeTu
//
//  Created by gy on 2023/12/6.
//

import Foundation


class MineVM {
    
    private(set) var items = [ListSectionItem]()
    private(set) var isShowExchange = false
    
    init() {
        updateItems()
    }
    
    /// 更新页面数据
    func updateItems() {
        
//        ListSectionItem(title: "优惠券".localized, img: kUserManager.isVip ? "set_coupon_black":"set_coupon_coloured"),
        
        let black = "_coloured"//"_black"
        let coloured = "_coloured"
        
        items = [
            ListSectionItem(title: "填写好友邀请码".localized, img: kUserManager.isVip ? "set_invited\(black)":"set_invited\(coloured)"),
                 ListSectionItem(title: "购买记录".localized, img: kUserManager.isVip ? "set_purchase_history\(black)":"set_purchase_history\(coloured)"),
                 ListSectionItem(title: "我的收藏".localized, img: kUserManager.isVip ? "set_collect\(black)":"set_collect\(coloured)"),
                 ListSectionItem(title: "意见反馈".localized, img: kUserManager.isVip ? "set_feedback\(black)":"set_feedback\(coloured)", isShowLineView: false)]
        
        guard isShowExchange else { return }
        items.insert(ListSectionItem(title: "激活卡".localized, img: kUserManager.isVip ? "set_conversion_black":"set_conversion_coloured"), at: 1)
    }
    
    /// 获取兑换显示
    func getRedemptionSetting(completeHandle: @escaping NetworkRequestHandle) {
        
        apiProvider.request(api: .redemptionSetting) { [weak self] json, jsonArr, code in
            MYLog("获取兑换显示：", json)
            var dialog = DialogMessage()
            guard let strongSelf = self, code == 200, let resp = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = resp.code
            dialog.message = resp.msg
            guard code == 200, let data = resp.data as? [String: Any], let show = data["show"] as? Bool else { return completeHandle(dialog) }
            strongSelf.isShowExchange = show
            strongSelf.updateItems()
            completeHandle(dialog)
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }

    }
    
}
